package com.yaofeng.mybatis.modules.sys.controller;


import com.yaofeng.mybatis.modules.sys.vo.SelectTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.yaofeng.mybatis.modules.sys.service.IDeptService;
import com.yaofeng.mybatis.modules.sys.entity.Dept;
import com.yaofeng.mybatis.annotation.Log;
import com.yaofeng.mybatis.common.ResponseInfo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author yaofeng
 * @since 2019-11-05
 */
@RestController
@RequestMapping("/sys/dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    @GetMapping("/list")
    @RequiresAuthentication
    @Log(value = "部门列表")
    public ResponseInfo list(@RequestParam Map<String, Object> params) {
        List<SelectTreeVo> treeVoList  = deptService.buildSelectTreeNode(params);
        return ResponseInfo.success(treeVoList);
    }

     /**
     * 保存部门
     * @param dept
     * @return
     */
    @PostMapping("/save")
    @RequiresAuthentication
    @Log(value = "保存部门")
    public  ResponseInfo save(@RequestBody Dept dept){
        deptService.saveDept(dept);
        return ResponseInfo.success();
    }


    /**
     * 更新部门
     * @param dept
     * @return
     */
    @PutMapping("/update")
    @RequiresAuthentication
    @Log(value = "更新部门")
    public  ResponseInfo update(@RequestBody  Dept dept){
        deptService.updateDept(dept);
        return ResponseInfo.success();
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping ("/delete")
    @RequiresAuthentication
    @Log(value = "删除")
    public  ResponseInfo delete(Long id){
       deptService.deleteDept(id);
        return ResponseInfo.success();
    }


    @GetMapping("/tree/list")
    @RequiresAuthentication
    @Log(value = "部门列表")
    public ResponseInfo treeList(@RequestParam Map<String, Object> params) {
        // 只查询启用的部门
        //params.put("status",0);
        List<SelectTreeVo> treeVoList  = deptService.buildSelectTreeNode(params);
        return ResponseInfo.success(treeVoList);
    }

}
